package com.atguigu.springdgademo.meta.service.impl;

import com.atguigu.springdgademo.meta.bean.TableMetaInfo;
import com.atguigu.springdgademo.meta.bean.TableMetaInfoExtra;
import com.atguigu.springdgademo.meta.mapper.TableMetaInfoExtraMapper;
import com.atguigu.springdgademo.meta.service.TableMetaInfoExtraService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import static com.atguigu.springdgademo.constant.DgaConstant.*;

/**
 * <p>
 * 元数据表附加信息 服务实现类
 * </p>
 *
 * @author wumingshi
 * @since 2024-08-29
 */
@Service
@DS("mydga")
public class TableMetaInfoExtraServiceImpl extends ServiceImpl<TableMetaInfoExtraMapper, TableMetaInfoExtra> implements TableMetaInfoExtraService {

    @Autowired
    private TableMetaInfoExtraMapper tableMetaInfoExtraMapper;


    @Override
    public void initTableMetaInfoExtra(ArrayList<TableMetaInfo> tableMetaInfos) {
        ArrayList<TableMetaInfoExtra> tableMetaInfoExtras = new ArrayList<>();
        for (TableMetaInfo tableMetaInfo : tableMetaInfos) {
            //查询表中是否有当前表的信息
            TableMetaInfoExtra tableMetaInfoExtra = this.getOne(
                    new QueryWrapper<TableMetaInfoExtra>()
                            .eq("schema_name", tableMetaInfo.getSchemaName())
                            .eq("table_name", tableMetaInfo.getTableName())
            );

            if(tableMetaInfoExtra == null){
                //当前表没有被初始化过
                tableMetaInfoExtra = new TableMetaInfoExtra();
                //库名
                tableMetaInfoExtra.setSchemaName(tableMetaInfo.getSchemaName());
                //表名
                tableMetaInfoExtra.setTableName(tableMetaInfo.getTableName());
                //技术owner
                tableMetaInfoExtra.setTecOwnerUserName(TEC_OWNER_UNSET);
                //业务owner
                tableMetaInfoExtra.setBusiOwnerUserName(BUSI_OWNER_UNSET);
                //生命周期类型
                tableMetaInfoExtra.setLifecycleType(LIFECYCLE_TYPE_UNSET);
                //生命周期天数
                tableMetaInfoExtra.setLifecycleDays(LIFECYCLE_DAYS_UNSET);
                //安全级别
                tableMetaInfoExtra.setDwLevel(getDwlevel(tableMetaInfo.getTableName().toUpperCase()));
                //创建时间
                tableMetaInfoExtra.setCreateTime(new Date());

                //赞批次
                tableMetaInfoExtras.add(tableMetaInfoExtra);
            }
        }
        //写数据
        this.saveBatch(tableMetaInfoExtras);
    }

    private String getDwlevel(String tableName) {
        if(tableName.startsWith(DW_LEVEL_ODS)){
            return DW_LEVEL_ODS;
        } else if (tableName.startsWith(DW_LEVEL_DWD)) {
            return DW_LEVEL_DWD;
        }else if (tableName.startsWith(DW_LEVEL_DIM)) {
            return DW_LEVEL_DIM;
        }else if (tableName.startsWith(DW_LEVEL_DWS)) {
            return DW_LEVEL_DWS;
        }else if (tableName.startsWith(DW_LEVEL_ADS)) {
            return DW_LEVEL_ADS;
        }else if (tableName.startsWith(DW_LEVEL_DM)) {
            return DW_LEVEL_DM;
        }else  {
            return DW_LEVEL_OTHER;
        }
    }
}
